import Vue from 'vue';
import iView from 'iview';
import VueRouter from 'vue-router'
import util from '../libs/util'
import Cookies from 'js-cookie'
// import HelloWorld from '@/views/HelloWorld'
import { routers, loginRouter, appRouter } from './router'

Vue.use(VueRouter)

const RouterConfig = {
  mode: 'history',
  routes: routers
}

const router = new VueRouter(RouterConfig)

export default router

router.beforeEach((to, from, next) => {
  util.title(to.meta.title)
  // iView.LoadingBar.start()
  if ((!Cookies.get('token') || !Cookies.get('userInfo')) && to.name !== 'login') { // 判断是否已经登录且前往的页面不是登录页
    next({
      name: 'login'
    })
  } else {
    /*
    let position = window.scrollY
    console.log(position)
    // 离开路由时把位置存起来
    this.$store.commit('savePosition', position)
    */
    next(true)
  }
})

router.afterEach((to) => {
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})
